<script src="__STATIC__/plugins/datatables/js/jquery.dataTables.min.js"></script>


<div class="row">
    <div class="col-md-12">
        <div class="box box-solid">
            <div class="table-responsive">
                <table id="tb1" class="table table-bordered datatable" width="100%">
                    <thead>
                    <tr>
                        <td>客户姓名：{$user.name}</td>
                        <td>电话：{$user.mobile}</td>
                        <td>日期：{$order.create_time|date="Y-m-d",###}</td>
                        <td colspan="2">地址：{$user.address}</td>
                    </tr>
                    </thead>
                </table>
            </div>
            <div class="box-body">
                <div class="box-header with-border" id='addGoods'>
                    <h3  class="box-title">添加商品
                        <a href="/admin/order/add_goods?order_id={$order.id}"
                           class="btn btn-primary btn-xs" data-toggle="tooltip" title="新增商品">
                            <i class="fa fa-plus"></i>
                        </a></h3>
                </div>
                <table id="example" class="display" cellspacing="0" style="width:100%">

                </table>
                <!--<tfoot id="print">
                <tr><td colspan="5" class="text-center"><input class="btn btn-default noprint" type="submit" onclick="showhide();" value="打印"></td></tr>
                </tfoot>-->

            </div>
            <!-- /.box-body -->
        </div>
    </div>

</div>

<script>
    var order_id = "{$order.id}";
    console.info(order_id);
        var table = $('#example').DataTable(
            {
                info: false,    // 左下角信息去掉
                ordering: false,   // 排序去掉
                "paging": false,
                "searching": false,  // 关闭搜索
                serverSide: true,
                "sAjaxSource": "/api/order/orderList?order_id="+order_id,
                "fnServerData": function (sSource, aoData, fnCallback, oSettings) {
                    oSettings.jqXHR = $.ajax({
                        "dataType": 'json',
                        "type": "POST",
                        "url": sSource,
                        "data": aoData,
                        "success": function (data) {
                            fnCallback(data);
                        }
                    })
                },
                "columns": [
                    {
                        "data": "goods_all.name",
                        title: '商品名称',
                        "defaultContent": '--',
                        render:function(data,type,row,meta){
                            if(data==null ){
                                return data ='合计：';
                            }else{
                                return data;
                            }
                        }
                    },
                    {
                        "data": "goods_all.advance_price",
                        title: '预售价钱', "defaultContent": '--',
                    },
                    {
                        "data": "price",
                        title: '商品价钱', "defaultContent": '--'
                    },
                    {"data": "goods_all.goods_spe", title: '商品规格', "defaultContent": '--'},
                    {
                        "data": "goods_num",
                        title: '购买数量', "defaultContent": '--',
                    },
                    {
                        "data": "remark",
                        title: '备注', "defaultContent": '--',
                    },
                    {
                        "data": "subtotal",
                        title: '金额', "defaultContent": '--',
                        render:function(data,type,row,meta){
                            if(data==null ){
                                return data = row.total;
                            }else{
                                return data;
                            }
                        }

                    }
                ],
                "columnDefs": [
                    {
                        "targets": [2, 4,5],
                        createdCell: function (cell, cellData, rowData, rowIndex, colIndex) {
                            $(cell).click(function () {
                                $(this).html('<input type="text" size="16" style="width: 100%"/>');
                                var aInput = $(this).find(":input");
                                aInput.focus().val(cellData);
                            });
                            $(cell).on("blur", ":input", function () {

                                var text = $(this).val();
                                $(cell).html(text);
                                table.cell(cell).data(text)

                                // 更新后台数据

                                $.ajax({
                                    "url": "/api/order/edit",
                                    "data": rowData,
                                    "type": "post",
                                     success : function(data){
                                         onRefresh();
                                     },
                                     error:function(data){
                                         alert("新增失败");
                                     }
                                });
                            })
                        }
                    },
                ],
            }
        );


        function showhidediv(id) {
            var sbtitle = document.getElementById(id);
            if(sbtitle) {
                if(sbtitle.style.display == 'block'|| sbtitle.style.display == '') {
                    sbtitle.style.display = 'none';
                } else {
                    sbtitle.style.display = 'block';
                }
            }
        }

        function showhide(){
            showhidediv("addGoods");
            showhidediv("print");
            window.print();
        }

        // 重新加载table数据
        function onRefresh(){
            table.ajax.reload();

        }
</script>
